package com.xywzxw.niushop.entity;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.HashMap;

@AllArgsConstructor
@Data
public class CommonResult<T> {
    private long code;

    private String message;

    private T data;

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> CommonResult<T> success(T data) {
        Object object = data;
        if (data == null){
            object = new HashMap<>();
        }
        return new CommonResult<T>(200, "success", (T) object);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        HashMap<String, String> map = new HashMap<>();
        return new CommonResult<T>(500, message,  (T) map);
    }

    /**
     * 参数校验失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> CommonResult<T> checkFailed(String message) {
        HashMap<String, String> map = new HashMap<>();
        return new CommonResult<T>(400, message, (T) map);
    }
}
